package com.sprouting.ops.filter.service;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HtmlUtil;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * XSS攻击过滤
 *
 * @author ：LX（长沙麓谷）
 * @date 创建时间： 2023/3/16 20:37
 */
public class XssHttpServletRequestWrapper extends HttpServletRequestWrapper {

    /**
     * 构造一个封装给定请求的请求对象
     *
     * @param request The request to wrap
     * @throws IllegalArgumentException if the request is null
     */
    public XssHttpServletRequestWrapper(HttpServletRequest request) {
        super(request);
    }


    /**
     * 获取参数
     * @param name 参数
     * @return 被xss过滤后的参数
     */
    @Override
    public String getParameter(String name) {
        String parameter = super.getParameter(name);
        if (StrUtil.isNotBlank(parameter)){
            // xss过滤
            parameter = HtmlUtil.filter(parameter);
        }
        return parameter;
    }

    /**
     * 获取参数值
     * @param name 参数值
     * @return
     */
    @Override
    public String[] getParameterValues(String name) {
        String[] parameterValues = super.getParameterValues(name);
        if (parameterValues != null){
            for (int i = 0; i < parameterValues.length; i ++){
                String value = parameterValues[i];
                if (StrUtil.isNotBlank(value)){
                    // xss过滤
                    value = HtmlUtil.filter(value);
                }
                parameterValues[i] = value;
            }
        }

        return parameterValues;
    }

    /**
     * 获取map 形式参数
     * @return
     */
    @Override
    public Map<String, String[]> getParameterMap() {
        Map<String, String[]> parameterMap = super.getParameterMap();
        LinkedHashMap<String, String[]> map = new LinkedHashMap<>();
        if (parameterMap != null){
            for (String key : parameterMap.keySet()){
                String[] valueArray = parameterMap.get(key);
                for (int k = 0; k < valueArray.length; k ++){
                    String value = valueArray[k];
                    if (StrUtil.isNotBlank(value)){
                        // xss过滤
                        value = HtmlUtil.filter(value);
                    }
                    valueArray[k] = value;
                }
                map.put(key, valueArray);
            }
        }

        return map;
    }

    /**
     * 请求头过滤
     * @param name 参数
     * @return
     */
    @Override
    public String getHeader(String name) {
        String header = super.getHeader(name);
        if (StrUtil.isNotBlank(header)){
            // xss过滤
            header = HtmlUtil.filter(header);
        }
        return header;
    }

    /**
     * 对输入流进行过滤
     * @return
     * @throws IOException
     */
    @Override
    public ServletInputStream getInputStream() throws IOException {
        // 流不进行过滤
        return super.getInputStream();
    }
}
